// import axios from '@/utils/request'
var format = require('date-format')
// const SETTLE_STATUS = {
//   0: '未分账',
//   1: '已分账'
// }
// const TYPES = {
//   0: '投资人',
//   1: '管理员分成',
//   2: '平台费用',
//   3: '服务站',
//   4: '采购成本',
//   5: '资源公司分成',
//   6: '上级业务',
//   7: '公司(团队)',
//   8: '业务提成',
//   9: '补贴',
//   10: '工资',
//   11: '订台分成',
//   12: '服务员分成',
//   100: '剩余结余',
//   999: '平台手续费'
// }
export default {
  SettleItem: {
    form: {
      fields: [
        'status',
        'paidTime'
      ]
    },
    list: {
      export: {
        query: {
          '@display': `{
                      'store' : 'entity.getSettle().getOrderItem().getOrder().getStore().__toString()',
                      'outTradeNo': 'entity.getSettle().getOrderItem().getOrder().getInvoice().getOutTradeNo()',
                      'counter' : 'entity.getSettle().getOrderItem().getOrder().getCounter().__toString()',
                      'staff': 'entity.getSettle().getOrderItem().getOrder().getStaff().getName()',
                      'product' : 'entity.getSettle().getOrderItem().getSpecification().getProduct().getName()',
                      'quantity' : 'entity.getSettle().getOrderItem().getQuantity()',
                      'price' : 'entity.getSettle().getOrderItem().getPrice()',
                      'profitType' : 'entity.getProfitType()',
                      'amount' : 'entity.getAmount()',
                      'acc': 'entity.getStaff() ? entity.getStaff().__toString() : entity.getUser().__toString()',
                      'settled' : '{0: "未分账",1: "已分账"}[entity.getSettled()]',
                      'orderTime' : 'entity.getSettle().getOrderItem().getOrder().getCreatedTime().format("Y-m-d H:i:s")',
                      'createdTime' : 'entity.getCreatedTime().format("Y-m-d H:i:s")',
                      'status': 'entity.getSettle().getOrderItem().getOrder().getStatus()'
                  }`,
          'limit': 100000000
        },
        label: {
          'store': '门店',
          'outTradeNo': '支付单号',
          'counter': '台号',
          'staff': '业务员',
          'product': '产品',
          'quantity': '数量',
          'price': '总价',
          'profitType': '类型',
          'amount': '分账金额',
          'acc': '分账账号',
          'settled': '分账状态',
          'orderTime': '下单时间',
          'createdTime': '分账时间',
          'status': '订单状态'
        }
      },
      disabled_actions: ['new', 'lines', 'edit', 'delete', 'export'],
      query: {
        '@order': 'entity.id|DESC',
        '@expands': "['entity.settle.orderItem.order','entity.settle.orderItem.specification']"
      },
      list_display: [
        'id',
        { property: 'settle.__metadata.orderItem.__metadata.order.__metadata.store.__toString', label: '门店' },
        // { property: 'settle.__metadata.orderItem.__metadata.order.__metadata.counter.__toString', label: '台号' },
        { property: 'settle.__metadata.orderItem.__metadata.specification.__metadata.product.__toString', label: '产品' },
        { property: 'settle.__metadata.orderItem.__metadata.quantity', label: '数量' },
        { property: 'settle.__metadata.orderItem.__metadata.price', label: '总价' },
        {
          property: 'profitType', label: '类型',
        },
        {
          property: 'amount', label: '分账金额',
          component: {
            props: ['data'],
            render(h) {
              return <span>￥{this.data}</span>
            }
          }
        },
        {
          property: 'acc', label: '分账账号',
          component: {
            props: ['record'],
            data() {
              return {
                name: ''
              }
            },
            created() {
              if (this.record.staff) {
                this.name = this.record.staff.__toString
              } else {
                this.axios.get(`/manage/staffs`, {
                  params: {
                    '@filter': `entity.getStore() == ${this.record.settle.__metadata.orderItem.__metadata.order.__metadata.store.id} && entity.getUser().getId() == ${this.record.user.id}`
                  }
                }).then((res) => {
                  if (res.data.length > 0) {
                    console.log(res.data)
                    this.name = res.data[0].name
                  } else {
                    console.log('无staff')
                    this.name = this.record.user.__toString
                  }
                })
              }
            },
            render(h) {
              // console.log(this.record)
              return <span>{this.name}</span>
            }
          }
        },
        {
          property: 'settled', label: '分账状态',
          component: {
            props: ['data', 'record'],
            render(h) {
              if (this.data) {
                return <el-tag type='success'>已分账</el-tag>
              } else {
                return <el-tag type='warning'
                  onClick={() => {
                    console.log(this.record)
                    const orderId = this.record.settle.__metadata.orderItem.__metadata.order.id
                    console.log(orderId)
                    this.axios.post(`/manage/orders/${orderId}/settle-execute`).then((res) => {
                      console.log(res.data)
                      this.$message.success('分账成功！')
                      this.record.settled = true
                    })
                  }}
                >未分账</el-tag>
              }
            }
          }
        },
        {
          property: 'settle.__metadata.orderItem.__metadata.order.__metadata.createdTime',
          label: '订单创建时间',
          component: {
            props: ['data'],
            data() {
              return { createdTime: format('yyyy-MM-dd hh:mm:ss', new Date(this.data)) }
            },
            render(h) {
              return <span>{this.createdTime}</span>
            }
          }
        },
        'createdTime'
      ]
    }
  }
}

